package com.csdj.t309.geamtest.controller;


import com.csdj.t309.geamtest.common.WebResult;
import com.csdj.t309.geamtest.common.WebResultMsgCode;
import com.csdj.t309.geamtest.entity.User;
import com.csdj.t309.geamtest.util.HttpRestUtil;
import com.csdj.t309.geamtest.util.WeixinUtils;
import com.csdj.t309.geamtest.vo.WxAccessRespVo;
import com.csdj.t309.geamtest.vo.YongHu;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

/**
     * @ClassName weixin
     * @Author zhangwen
     * @Description 测试号接入
     * @Date 2019/12/16 15:39
     */
    @RestController
    @RequestMapping("/weixin")
    public class WeiXin extends BaseController{
        private String wxAppId = "wx61b5e2d66becf7c0";
        private String wxappsecret = "a6780ce801f57bd6708d0cce20605d90";
        private final static String LOGIN_CALLBACK="http://y416079762.oicp.vip/weixin/login/callback";

        @Autowired
        private WeixinUtils weixinservice;

        @GetMapping("/wx")
        public String wx(@RequestParam("signature")String signature,
                         @RequestParam("timestamp")String timestamp,
                         @RequestParam("nonce")String nonce,
                         @RequestParam("echostr")String echostr){
            //获取微信服务器发来的参数，并将token、timestamp、nonce三个参数进行字典序排序
            //将三个参数字符串拼接成一个字符串进行sha1加密
            //开发者获得加密后的字符串可与signature对比，标识该请求来源于微信
            //check方法用来排序以及比较
            if (weixinservice.check(timestamp,nonce,signature)){
                return echostr;
            }
            return null;
        }

        /**
         * 授权回调接口
         * @param code
         * @param state
         * @param request
         * @return
         */
        @GetMapping("/login/callback")
        public String loginCallback(String code, String state, HttpServletRequest request){
            //获取access_code
            String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid="+wxAppId+"&secret="+wxappsecret+"&code="+code+"&grant_type=authorization_code";
            String resp = HttpRestUtil.get(url, String.class);
            ObjectMapper om = new ObjectMapper();
            WxAccessRespVo accessResp = null;
            try {
                accessResp = om.readValue(resp, WxAccessRespVo.class);
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
            if(accessResp != null && StringUtils.hasText(accessResp.getAccess_token())){
                String openids = accessResp.getOpenid();
                String tokens = accessResp.getAccess_token();
                System.out.println(tokens);
                String xenix = "https://api.weixin.qq.com/cgi-bin/user/info?access_token="+tokens+"&openid="+openids;
                String reaps = HttpRestUtil.get(xenix, String.class);
                YongHu yongHu = null;
                try {
                    yongHu = om.readValue(reaps, YongHu.class);
                } catch (JsonProcessingException e) {
                    e.printStackTrace();
                }
                if(yongHu != null){
                    String nickName = yongHu.getNickname();
                    return nickName;
                }
            }
            return null;
        }

        @GetMapping("/login/qrcode")
        public WebResult getLoginUrl(){
            String qrcode = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx61b5e2d66becf7c0&redirect_uri=http://y416079762.oicp.vip/weixin/login/callback&response_type=code&scope=snsapi_base&state=123#wechat_redirect" ;
            return resp(WebResultMsgCode.SUCCESS,qrcode);
        }


}